package com.xyb.controller;

import com.xyb.client.SmsRegClient;
import com.xyb.config.exception.MyException;
import com.xyb.entity.Result;
import com.xyb.entity.StatusCode;
import com.xyb.service.RegisterService;
import com.xyb.vo.RegisterUserVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("reg")
public class RegisterController {
    @Resource
    private RegisterService registerService;
    @Resource
    private SmsRegClient smsRegClient;

    @PostMapping("user")
    public String insertUser(@RequestBody @Validated(RegisterUserVo.reg.class) RegisterUserVo vo) {
        //校验手机号是否已经注册
        registerService.selectIphoneIsRegistered(vo);
        //校验手机号的验证码是否正确
        Result result = smsRegClient.selectRegisterCodeByIphoneNo(vo.getIphoneNo(), vo.getVerifyCode());
        boolean flag = (boolean) result.getData();  //验证码是否有效
        if(!flag){
            throw new MyException(StatusCode.VERIFY_CODE_EXCEPTION); //验证码无效
        }
        return registerService.insertUser(vo);
    }
}